﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
using System.IO;

namespace nl.tno.ict.multitouch.wpfmultitouch.events
{
    public static class XMLSettingsReader
    {
        public static double RECOGNISEPERCENTAGE;
        public static string CONNECTIONIP;
        public static int CONNECTIONPORT;
        public static string CLICKIMAGE;
        public static string DRAGIMAGE;
        public static string SCALEIMAGE;
        public static string ROTATEIMAGE;
        public static string SHOWKEYBOARDIMAGE;

        /// <summary>
        /// Parses the XML settingsfile node by node, and set the value of each static property configured in this file.
        /// </summary>
        public static void ReadXMLSettings()
        {
            try
            {
                if (Directory.GetFiles("./", "config.xml").Length < 1)
                    createXML();
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load("config.xml");
                XmlElement root = xmldoc.DocumentElement;
                XmlNodeList nodes = root.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    switch (node.Name)
                    {
                        case "Gestures":
                            try { RECOGNISEPERCENTAGE = double.Parse(node["RecognisePercentage"].InnerText, new CultureInfo("en-US")); }
                            catch { RECOGNISEPERCENTAGE = 0.80; }
                            break;
                        case "nl.tno.ict.multitouch.framework.controls":
                            try { CLICKIMAGE = node["ClickImage"].InnerText; }
                            catch { CLICKIMAGE = @"Adorner\Icons\Click.gif"; }
                            try { DRAGIMAGE = node["DragImage"].InnerText; }
                            catch { DRAGIMAGE = @"Adorner\Icons\Drag.gif"; }
                            try { SCALEIMAGE = node["ScaleImage"].InnerText; }
                            catch { SCALEIMAGE = @"Adorner\Icons\Scale.gif"; }
                            try { ROTATEIMAGE = node["RotateImage"].InnerText; }
                            catch { ROTATEIMAGE = @"Adorner\Icons\Rotate.gif"; }
                            try { SHOWKEYBOARDIMAGE = node["KeyboardImage"].InnerText; }
                            catch { SHOWKEYBOARDIMAGE = @"Adorner\Icons\ShowKeyboard.gif"; }
                            break;
                        case "MTDebug":
                            break;
                        case "MTManager":
                            break;
                        case "TouchLib":
                            try { CONNECTIONIP = node["ConnectionIP"].InnerText; }
                            catch { CONNECTIONIP = "127.0.0.1"; }
                            try { CONNECTIONPORT = int.Parse(node["ConnectionPort"].InnerText); }
                            catch { CONNECTIONPORT = 3001; }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine("Adorners not found!");
            }
        }

        /// <summary>
        /// Creates the XML configuration file.
        /// </summary>
        private static void createXML()
        {
            XmlTextWriter textWriter = new XmlTextWriter("config.xml", Encoding.UTF8);
            textWriter.Formatting = Formatting.Indented;
            textWriter.WriteStartDocument();
            
            textWriter.WriteStartElement("Settings");
            
            textWriter.WriteStartElement("Gestures");
            textWriter.WriteStartElement("RecognisePercentage");
            textWriter.WriteString("0.8");
            textWriter.WriteEndElement();
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("nl.tno.ict.multitouch.framework.controls");
            textWriter.WriteStartElement("ClickImage");
            textWriter.WriteString(@"Adorner\Icons\Click.gif");
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("DragImage");
            textWriter.WriteString(@"Adorner\Icons\Drag.gif");
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("ScaleImage");
            textWriter.WriteString(@"Adorner\Icons\Scale.gif");
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("RotateImage");
            textWriter.WriteString(@"Adorner\Icons\Rotate.gif");
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("KeyboardImage");
            textWriter.WriteString(@"Adorner\Icons\ShowKeyboard.gif");
            textWriter.WriteEndElement();
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("MTDebug");
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("MTManager");
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("TouchLib");
            textWriter.WriteStartElement("ConnectionIP");
            textWriter.WriteString("127.0.0.1");
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("ConnectionPort");
            textWriter.WriteString("3001");
            textWriter.WriteEndElement();
            textWriter.WriteEndElement();

            textWriter.WriteEndElement();

            textWriter.WriteEndDocument();
            textWriter.Close();
        }
    }
}
